package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Brand;
import com.jxc.model.po.Discount;
import com.jxc.model.po.Goods;
import com.jxc.model.po.GoodsType;
import com.jxc.model.po.Repertory;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VGoods;
import com.jxc.service.GoodsServiceI;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsServiceI {

	@Autowired
	private BaseDaoI<Goods> goodsDao;

	@Override
	public List<VGoods> combobox(VGoods vgoods) {
		List<VGoods> vgoodsz = new ArrayList<VGoods>();
		vgoodsz.addAll(this.changeModel(this.find(vgoods)));
		return vgoodsz;
	}

	@Override
	public DataGrid datagrid(VGoods vgoods) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vgoods)));
		dg.setTotal(this.total(vgoods));
		return dg;
	}

	@Override
	public DataGrid unsaleDatagrid(VGoods vgoods) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.findunsalableGoods(vgoods)));
		dg.setTotal(this.total(vgoods));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Goods goods = goodsDao.get(Goods.class, Long.parseLong(id.trim()));
					if (goods != null) {
						goods.setIsDelete(true);
						goodsDao.update(goods);
					}
				}
			}
		}
	}

	@Override
	public void updateLastSaleDate(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Goods goods = goodsDao.get(Goods.class, Long.parseLong(id.trim()));
					if (goods != null) {
						goods.setLastedSaleTime(new Date());
						goodsDao.update(goods);
					}
				}
			}
		}
	}

	@Override
	public void save(VGoods vgoods) {
		Goods goods = new Goods();
		BeanUtils.copyProperties(vgoods, goods);
		if (vgoods.getRid() != null) {
			goods.setRepertory(new Repertory(vgoods.getRid()));
		}
		if (vgoods.getBid() != null) {
			goods.setBrand(new Brand(vgoods.getBid()));
		}
		if (vgoods.getDid() != null) {
			goods.setDiscount(new Discount(vgoods.getDid()));
		}
		if (vgoods.getGtid() != null) {
			goods.setGoodsType(new GoodsType(vgoods.getGtid()));
		}
		this.goodsDao.save(goods);
	}

	@Override
	public void update(VGoods vgoods) {
		Goods goods = goodsDao.get(Goods.class, vgoods.getId());
		BeanUtils.copyProperties(vgoods, goods, new String[] { "id", "isDelete","lastedSaleTime","initAmount","orderTotal","orderReturnTotal","saleTotal","saleReturnTotal","damageTotal","transferTotal","repertoryTotal" });
		if (vgoods.getRid() != null) {
			goods.setRepertory(new Repertory(vgoods.getRid()));
		}
		if (vgoods.getBid() != null) {
			goods.setBrand(new Brand(vgoods.getBid()));
		}
		if (vgoods.getDid() != null) {
			goods.setDiscount(new Discount(vgoods.getDid()));
		}
		if (vgoods.getGtid() != null) {
			goods.setGoodsType(new GoodsType(vgoods.getGtid()));
		}
		this.goodsDao.update(goods);
	}

	private List<VGoods> changeModel(List<Goods> goodsz) {
		List<VGoods> vgoodsz = new ArrayList<VGoods>();
		if (goodsz != null && goodsz.size() > 0) {
			for (Goods goods : goodsz) {
				VGoods vgoods = new VGoods();
				BeanUtils.copyProperties(goods, vgoods);
				if (goods.getRepertory() != null) {
					Repertory repertory = goods.getRepertory();
					vgoods.setRid(repertory.getId());
					vgoods.setRname(repertory.getName());
				}
				if (goods.getBrand() != null) {
					Brand brand = goods.getBrand();
					vgoods.setBid(brand.getId());
					vgoods.setBname(brand.getName());
				}
				if (goods.getGoodsType() != null) {
					GoodsType goodsType = goods.getGoodsType();
					vgoods.setGtid(goodsType.getId());
					vgoods.setGtname(goodsType.getName());
				}
				if (goods.getDiscount() != null) {
					Discount discount = goods.getDiscount();
					vgoods.setDid(discount.getId());
					vgoods.setDiscountNum(discount.getDiscountNum());
				}
				vgoodsz.add(vgoods);
			}
		}
		return vgoodsz;
	}

	private List<Goods> find(VGoods vgoods) {
		String hql = "from Goods t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vgoods, hql, values);

		if (vgoods.getSort() != null && vgoods.getOrder() != null) {
			hql += " order by " + vgoods.getSort() + " " + vgoods.getOrder();
		}
		return goodsDao.find(hql, values, vgoods.getPage(), vgoods.getRows());
	}

	private List<Goods> findunsalableGoods(VGoods vgoods) {
		String hql = "from Goods t where 1=1  and t.lastedSaleTime <= ? ";

		List<Object> values = new ArrayList<Object>();
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -3);
		values.add(cal.getTime());
		hql = addWhere(vgoods, hql, values);

		if (vgoods.getSort() != null && vgoods.getOrder() != null) {
			hql += " order by " + vgoods.getSort() + " " + vgoods.getOrder();
		}
		return goodsDao.find(hql, values, vgoods.getPage(), vgoods.getRows());
	}

	private Long total(VGoods vgoods) {
		String hql = "select count(*) from Goods t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vgoods, hql, values);
		return goodsDao.count(hql, values);
	}

	private String addWhere(VGoods vgoods, String hql, List<Object> values) {
		if (vgoods.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vgoods.getId());
		}
		if (vgoods.getBarcode() != null && !"".equals(vgoods.getBarcode().trim())) {
			hql += " and t.barcode like ? ";
			values.add("%%" + vgoods.getBarcode() + "%%");
		}
		if (vgoods.getType() != null && !"".equals(vgoods.getType().trim())) {
			hql += " and t.type like ? ";
			values.add("%%" + vgoods.getType() + "%%");
		}
		if (vgoods.getColor() != null && !"".equals(vgoods.getColor().trim())) {
			hql += " and t.color like ? ";
			values.add("%%" + vgoods.getColor() + "%%");
		}
		if (vgoods.getSize() != null && !"".equals(vgoods.getSize().trim())) {
			hql += " and t.size like ? ";
			values.add("%%" + vgoods.getSize() + "%%");
		}
		if (vgoods.getGtid() != null) {
			hql += " and t.goodsType.id  = ? ";
			values.add(vgoods.getGtid());
		}
		if (vgoods.getBid() != null) {
			hql += " and t.brand.id  = ? ";
			values.add(vgoods.getBid());
		}
		if (vgoods.getRid() != null) {
			hql += " and t.repertory.id = ? ";
			values.add(vgoods.getRid());
		}
		hql += " and t.isDelete = 0 ";
		return hql;
	}

	@Override
	public List<VGoods> search(VGoods vgoods) {
		String hql = "from Goods t where t.isDelete=false  and t.repertory.id= ? and t.barcode=?";
		List<Object> values = new ArrayList<Object>();
		values.add(vgoods.getRid());
		values.add(vgoods.getBarcode());
		List<Goods> list = goodsDao.find(hql, values, 1, 1);
		List<VGoods> result = changeModel(list);
		return result;
	}

	@Override
	public Long count(VGoods vgoods) {
		return this.total(vgoods);
	}

}
